🐳 docker | July 19, 2021
dockerfile
의 개념과 컨테이너 이미지를 직접 만들어보고 배포해 봅시다.
도커란 개발한 애플리케이션(실행파일)과 운영환경이 모두 들어있는 독립된 공간
기술 면접에서 조금 더 전문적으로 말하기 위해 필자는 “독립된 환경에서 하나의 애플리케이션을 구동시키기 위한 코드와 그 모든 종속성을 패키징 하는 기술”이라고 연습했었다…
Polyglot
란?
다양한 언어로(해당 서비스를 구성할 때 최적화 된 환경을 구축하기 위한 언어)으로 개발하여 하나의 큰 서비스를 이루는 것을 의미합니다.
$ docker build -t hello.js:lastst . # dockerimage build
$ docker login # dockerhub login
$ docker push hello.js:latest # dockerhub에 push
$ mkdir build && cd build # 폴더 만들고 진입
$ cat > hello.js
const http = require('http');
const os = require('os');
console.log("Test server starting...");
var handler = function(request, response) {
console.log("Received request from " + request.connection.remoteAddress);
response.wirteHead(200);
response.end("Container Hostname: " + os.hostname() + "\n");
};
var www = http.createServer(handler);
www.listen(8080);
FROM node:12 # 베이스 이미지를 노드js로 지정
COPY hello.js / # 호스트 파일을 컨테이너 루트 디렉토리로 복사
CMD ["node", "/hello.js"] # 컨테이너 동작시 실행하는 코드
$ docker build -t hellojs:latest .
$ docker images # 이미지 생성 확인
$ mkdir webserver && cd webserver
FROM ubuntu:18.04
LABEL maintainer="nickhealthy <alskadmlcraz1@gmail.com>"
# install apache
RUN apt-get update \
&& apt-get install -y apache2
RUN echo "TEST WEB" > /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-DFOREGROUND"]
$ docker build -t webserver:v1 .
$ docker run -d -p 80:80 --name web webserver:v1
$ docker login # 도커 허브 개인 레포지토리에 올리기 위한 로그인 절차
$ docker push -t <본인ID>/hellojs
$ docker push -t <본인ID>/webserver:v1 # 버전을 따로 명시하지 않아 latest인 경우 버전명은 생략 가능
$ cat > webpage.sh
#!/bin/bash
mkdir /htdocs
while :
do
/usr/games/fortune > /htdocs/index.html
sleep 10
done
FROM debian:latest
COPY webpage.sh /
RUN apt-get update \
&& apt-get install -y fortune
RUN ["chmod", "+x", "./webpage.sh"] # 해당 파일을 copy 후 실행 권한이 없어서 따로 추가해 둠
CMD ["./webpage.sh"]
$ docker build -t fortune:21.02 . # 도커 이미지 파일 생성
$ docker run -d --name fortune fortune:21.02 # 도커 파일 실행
$ docker exec -it fortune bash
# 쉘 스크립트에 작성한 경로 /htdocs/index.html 확인
$ cat /htdocs/index.html
정상적으로 실행되는 것을 확인할 수 있었습니다.